using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

using WyUnity.Utility.ExtentionMethods;

namespace WyUnity.Data.DataAccess
{
	public class ParameterProvider
	{
		private ISqlDialect _dialect;
		public ParameterProvider(ISqlDialect dialect, string prefix)
		{
			_dialect = dialect;
			_prefix = prefix;
		}

		private readonly string _prefix;
		private int _pnum;
		private readonly List<DbParameter> _params = new List<DbParameter>();
		public DbParameter[] GetParams()
		{
			return _params.ToArray();
		}
		public string CreateOne(object value)
		{
			var p = _dialect.CreateParameter();
			p.ParameterName = "@" + _prefix + _pnum++;
			if (value == null || value == DBNull.Value)
			{
				p.DbType = DbType.String;
				p.Value = DBNull.Value;
			}
			else
			{
				p.DbType = value.GetType().ToDbType();
				p.Value = value;
			}


			_params.Add(p);

			return p.ParameterName;
		}
	}
}